package com.goods.business.service.imp;

import com.goods.business.converter.HealthConverter;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.business.utils.TimeUtils;
import com.goods.common.model.business.Health;
import com.goods.common.response.ActiveUser;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.HealthVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HealthServiceImpl implements HealthService {

    @Autowired
    private HealthMapper healthMapper;

    //今日是否打卡
    @Override
    public HealthVO isReport() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long userId = activeUser.getUser().getId();
        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        List<Health> health = healthMapper.selectByExample(example);
        System.out.println(health);
        if (!CollectionUtils.isEmpty(health) && health.size()!=0) {
            for (Health oneHealth : health) {
                Date createTime = oneHealth.getCreateTime();
                if (TimeUtils.isToday(createTime)) {
                    HealthVO healthVO = HealthConverter.converterToHealthVO(oneHealth);
                    return healthVO;
                }
            }
        }
        return null;
    }

    //打卡
    @Override
    public void doReport(HealthVO healthVO) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long userId = activeUser.getUser().getId();
        Health health = new Health();
        health.setAddress(healthVO.getAddress());
        health.setUserId(userId);
        health.setSituation(healthVO.getSituation());
        health.setTouch(healthVO.getTouch());
        health.setPassby(healthVO.getPassby());
        health.setReception(healthVO.getReception());
        health.setCreateTime(new Date());
        healthMapper.insert(health);
    }

    //查看打卡历史记录
    @Override
    public Map getHistory(Map pageMap) {
        List<Health> healthList = healthMapper.selectAll();
        List<HealthVO> healthVOList = HealthConverter.converterToHealthVOList(healthList);
        String pageSize = (String) pageMap.get("pageSize");
        int pageSizeInt = Integer.parseInt(pageSize);
        String pageNum = (String) pageMap.get("pageNum");
        int pageNumInt = Integer.parseInt(pageNum);
        List<HealthVO> page = ListPageUtils.page(healthVOList, pageSizeInt, pageNumInt);
        Map result = new HashMap();
        result.put("rows",page);
        result.put("total",healthVOList.size());
        return result;
    }
}
